package com.gusha.controller;



import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gusha.utils.JsonResult;
import com.gusha.utils.Md5Util;
import com.gusha.utils.MySecureProtocolSocketFactory;
@CrossOrigin(origins="*",maxAge=3600)
@Controller
public class SendPhoneMsgController {
	
	
	@CrossOrigin(origins="*",maxAge=3600)
	@PostMapping("/test")
	@ResponseBody
	public  JsonResult sendmsg(String usertel) throws Exception {
		JsonResult jsonResult = new JsonResult(false);
		ProtocolSocketFactory fcty = new MySecureProtocolSocketFactory();
		Protocol.registerProtocol("https", new Protocol("https", fcty, 443));
		String responseMsg = "usertel";
		HttpClient client = new HttpClient();
		PostMethod post = new PostMethod(
				"https://api.capcplus.com/requestCaptchaClient.do");
		post.addRequestHeader("Content-Type",
				"application/x-www-form-urlencoded;charset=UTF-8");// 在头文件中设置转码
		//Random ra=new Random();	
			int a=(int)((Math.random()*9+1)*100000);
			String Verification=String.valueOf(a);			
			NameValuePair[] data = { 
			new NameValuePair("ywId","2b9c843c-55bc-408b-a655-a5b0c102d1e6_captcha"),
			new NameValuePair("authz",Md5Util.getMD5Code("2b9c843c-55bc-408b-a655-a5b0c102d1e6_captcha"+"13666205")),
			new NameValuePair("mobile", usertel),//手机号码
			new NameValuePair("smsTemplateId","6fa8d34e-81c5-4179-b4c7-9cb912f3794d"),//
			new NameValuePair("smsParam","{\"code\":\""+Verification+"\"}") ,
			new NameValuePair("msgId", "1")
	
		};
		
		post.setRequestBody(data);
		
		client.executeMethod(post);
		Header[] headers = post.getResponseHeaders();
		int statusCode = post.getStatusCode();
		System.out.println("statusCode:" + statusCode);
		for (Header h : headers) {
			System.out.println(h.toString());
		}
		String result = new String(post.getResponseBodyAsString());
		System.out.println(result);
		if(post.getStatusCode()==HttpStatus.SC_OK){
			responseMsg=post.getResponseBodyAsString().trim();
			}
		post.releaseConnection();
		jsonResult.setObj(Verification);
		jsonResult.setMsg("发送成功");
		jsonResult.setFlag(true);
		return jsonResult;
	}
	
	
	}
	 


